package com.example.widget;

import com.example.voip.R;
import com.example.voip.R.id;
import com.example.voip.R.layout;
import com.example.voip.R.style;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

public class CallDialog extends Dialog {

	private LinearLayout key1,key2,key3,key4,key5,key6,key7,key8,key9,key0,key11,key12;//key11:* key12:#
	private LinearLayout toCall;
	private Button backspace_btn;
	
	private Context mContext;
	private DisplayMetrics metrics;
	//数字屏幕内容
	private EditText num_screen;
	private int marginBottom;//与底部的间距
	
	public CallDialog(Context context,DisplayMetrics metrics,int marginBottom) {
		super(context,R.style.CallDialogStyle);
		//getWindow().setWindowAnimations(R.anim.call_dialog_enter);
		this.mContext = context;
		this.metrics = metrics;
		this.marginBottom = marginBottom;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setCanceledOnTouchOutside(true);
		setContentView(R.layout.activity_call);
		android.view.WindowManager.LayoutParams lp = getWindow().getAttributes();
		lp.width = metrics.widthPixels;
		lp.y = marginBottom;	//与底部间距
		lp.gravity = Gravity.BOTTOM;
		getWindow().setAttributes(lp);
		num_screen = (EditText)findViewById(R.id.num_screen);
		init();
	}
	
	class keyClickListener implements android.view.View.OnClickListener{
		@Override
		public void onClick(View view) {
			String tag = (String) view.getTag();
			String currentNum = num_screen.getText().toString();//获取当前的号码
			StringBuffer sb = new StringBuffer(currentNum);
			sb.append(tag);										//添加按下的号码
			num_screen.setText(sb.toString());		 			//重置进去
			num_screen.setSelection(sb.length());				//重置光标位置
		}
	}
	
	class btnClickListener implements android.view.View.OnClickListener{
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.to_call:	
				Toast.makeText(mContext, "拨打："+num_screen.getText().toString(), 500).show();
				break;

			case R.id.to_backspace:
				//拨号退格键
				String currentNum = num_screen.getText().toString();
				if(currentNum.length()>0){
					currentNum = currentNum.substring(0, currentNum.length()-1);
					num_screen.setText(currentNum);
					num_screen.setSelection(currentNum.length());				//重置光标位置
				}
				break;
				
			default:
				break;
			}
		}
	}
	
	private void init(){
		//拨号
		toCall = (LinearLayout)findViewById(R.id.to_call);
		//后退
		backspace_btn = (Button)findViewById(R.id.to_backspace);
		toCall.setOnClickListener(new btnClickListener());
		backspace_btn.setOnClickListener(new btnClickListener());
		
		/*初始化键盘*/
		key1 = (LinearLayout)findViewById(R.id.key_1);
		key2 = (LinearLayout)findViewById(R.id.key_2);
		key3 = (LinearLayout)findViewById(R.id.key_3);
		key4 = (LinearLayout)findViewById(R.id.key_4);
		key5 = (LinearLayout)findViewById(R.id.key_5);
		key6 = (LinearLayout)findViewById(R.id.key_6);
		key7 = (LinearLayout)findViewById(R.id.key_7);
		key8 = (LinearLayout)findViewById(R.id.key_8);
		key9 = (LinearLayout)findViewById(R.id.key_9);
		key0 = (LinearLayout)findViewById(R.id.key_0);
		key11 = (LinearLayout)findViewById(R.id.key_11);
		key12 = (LinearLayout)findViewById(R.id.key_12);
		key1.setOnClickListener(new keyClickListener());
		key2.setOnClickListener(new keyClickListener());
		key3.setOnClickListener(new keyClickListener());
		key4.setOnClickListener(new keyClickListener());
		key5.setOnClickListener(new keyClickListener());
		key6.setOnClickListener(new keyClickListener());
		key7.setOnClickListener(new keyClickListener());
		key8.setOnClickListener(new keyClickListener());
		key9.setOnClickListener(new keyClickListener());
		key0.setOnClickListener(new keyClickListener());
		key11.setOnClickListener(new keyClickListener());
		key12.setOnClickListener(new keyClickListener());
	}
	
}
